package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page20Model;
import com.kaizzi.data.jdl.service.CrmMemberService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.DoublePredicate;
import java.util.stream.Collectors;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/24
 */
@Component
@Slf4j
public class PageData20 {

    @Resource
    private CrmMemberService crmMemberService;

    public String getData(ExcelModel model){

        Page20 page20 = new Page20();
        List<Page20Model> page20s = model.getPage20s();

        log.info("page20 excel 获取:{}",page20s);

        String startDate = page20s.get(4).getId();
        String endDate = page20s.get(page20s.size() - 1).getId();
        List<String> dateList = DateUtil.getDateFormat20(startDate, endDate);
        //起始时间内所有日期的 gClub人数
        List<TempBeen> clubUsers     = crmMemberService.getClubUser(dateList.get(0), dateList.get(1));
        List<TempPage20> tempPage20s = new ArrayList<>();

        for (int i = 0; i < page20s.size(); i++) {
            Page20Model pageModel = page20s.get(i);
            if (i == 0){
                page20.setMonthKpi(getTempPage20Data(pageModel));
                continue;
            }
            if (i == 2){
                page20.setYearKpi(getTempPage20Data(pageModel));
                continue;
            }
            if (i < 4){
                continue;
            }

            String date = pageModel.getId();
            String tempDate = DateUtil.getDateFormat12(date);
            List<TempBeen> collect = clubUsers.stream().filter(been -> been.getTempName().equals(tempDate)).collect(Collectors.toList());
            long tempClub = collect.size()>0? collect.get(0).getTempCount():0;
            String utc = pageModel.getName1();
            double tempUtc = Double.parseDouble(utc.replace(",", ""));
            String rate = StringUtil.decimalFormat((double) tempClub / tempUtc * 100)+"%";
            TempPage20 tempPage20 = new TempPage20();
            tempPage20.setDate(date);
            tempPage20.setUtc(StringUtil.decimalFormat(Long.parseLong(utc.replace(",",""))));
            tempPage20.setClub(StringUtil.decimalFormat(tempClub));
            tempPage20.setRate(rate);

            tempPage20s.add(tempPage20);
        }
        page20.setTitle(LogicUtil.getTitle(model,20));
        page20.setDataPeriod(DateUtil.getFirstDay2LastSunday());
        page20.setUtcClubs(tempPage20s);
        return JsonUtil.serialize(page20);
    }

    private TempPage20Data getTempPage20Data(Page20Model pageModel){
        String[] split = DateUtil.getFirstDay2LastSunday().split("-");
        long utc = 0;
        String rate = pageModel.getRate();
        if (pageModel.getId().equals("2")){
            //ytd 实际kpi
            utc = crmMemberService.getUserCount(split[0] + " 00:00:00", split[1] + " 23:59:59", "utc");
            rate = StringUtil.decimalFormat(utc/Double.parseDouble(pageModel.getPlanKpi().replace(",",""))*100)+"%";
        }else {
            //mtd 实际kpi
            LocalDateTime lastSunday = DateUtil.getLastSunday();
            String mStart  = lastSunday.getYear() + "-"+ lastSunday.getMonthValue() + "-01 00:00:00";
            utc = crmMemberService.getUserCount(mStart,split[1] + " 23:59:59", "utc");
            rate = "-";
        }
        TempPage20Data tempPage20Data = new TempPage20Data();
        tempPage20Data.setId(pageModel.getId());
        tempPage20Data.setName1(pageModel.getName1());
        tempPage20Data.setName2(pageModel.getName2());
        tempPage20Data.setActualKpi(StringUtil.decimalFormat(utc));
        tempPage20Data.setPredictKpi(pageModel.getPlanKpi());
        tempPage20Data.setRate(rate);
        tempPage20Data.setRemark(pageModel.getRemark());
        return tempPage20Data;
    }

    @Data
    private static class TempPage20{
        private String date;
        private String utc;
        private String club;
        private String rate;
    }

    @Data
    private static class Page20{
        private String title;
        private String dataPeriod;
        private List<TempPage20> utcClubs;
        private TempPage20Data monthKpi;
        private TempPage20Data yearKpi;
    }

    @Data
    private static class TempPage20Data {
        private String id;
        private String name1;
        private String name2;
        private String actualKpi;
        private String predictKpi;
        private String rate;
        private String remark;
    }
}
